package com.masterclient.app.modules.goods.adapter;

import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.goods.ProductBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.commonbean.product.ShProductTag;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;
import com.masterclient.app.widget.PriceIntegralLayout;
import java.util.List;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class SignInRecyclerAdapter extends BaseQuickAdapter<ProductBean, BaseViewHolder> {


    private int width;
    private String[] strings;

    public void setStrings(String... strings) {
        this.strings = strings;
    }

    public SignInRecyclerAdapter() {
        super(R.layout.item_sign_in_good);
        this.strings = new String[]{};
        width = (int) ((ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(110)) / 2f);
    }

    @Override
    protected void convert(BaseViewHolder helper, ProductBean item) {

        ImageView ivGoods = helper.getView(R.id.iv_goods);
        TextView tvSubTitle = helper.getView(R.id.tv_sub_title);
        TextView tvCoinDeduct = helper.getView(R.id.tv_coin_deduct);
        TextView tvOfficialTxt = helper.getView(R.id.tv_official_txt);
        TextView tvOfficialPrice = helper.getView(R.id.tv_official_price);
        PriceIntegralLayout llPrice = helper.getView(R.id.ll_price);

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivGoods.getLayoutParams();
        layoutParams.height = width;
        layoutParams.width = width;
        ViewTransformUtil.glideImageView(mContext, item.getSh_image(), ivGoods, new Transformation[]{new CenterCrop(),
                        new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0, RoundedCornersTransformation.CornerType.TOP)},
                R.mipmap.defaults);
        tvSubTitle.setText(item.getSh_title());

        //金币抵扣
        List<ShProductTag> tags = item.getTags();
        if (tags != null && tags.size() > 0) {
            for (ShProductTag tag : tags) {
                if ("5".equals(tag.getType())) {
                    tvCoinDeduct.setVisibility(View.VISIBLE);
                    tvCoinDeduct.setText(tags.get(0).getName());
                } else {
                    tvCoinDeduct.setVisibility(View.INVISIBLE);
                }
            }

        } else {
            tvCoinDeduct.setVisibility(View.INVISIBLE);
        }
        //官方价
        if (item.getPriceTagBean() != null && !TextUtils.isEmpty(item.getPriceTagBean().getMarket_price_tag())) {
            tvOfficialTxt.setText(item.getPriceTagBean().getMarket_price_tag());
            if (!TextUtils.isEmpty(item.getSh_market_price())) {
                tvOfficialPrice.setText("¥" + item.getSh_market_price());
                tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                tvOfficialPrice.setVisibility(View.VISIBLE);
            }
            tvOfficialTxt.setVisibility(View.VISIBLE);
        } else {
            tvOfficialTxt.setVisibility(View.GONE);
            tvOfficialPrice.setVisibility(View.GONE);
        }
        //商品价格
        llPrice.setData("", item.getSh_show_price());
        llPrice.floadSize(0.7f);

        helper.itemView.setOnClickListener(v -> ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                .withString("source_page", strings.length > 0 ? strings[0] : "")
                .withString("source_module", strings.length > 1 ? strings[1] : "")
                .withString("source_element", strings.length > 2 ? strings[2] : "")
                .withString("source_position", helper.getAdapterPosition() - getHeaderLayoutCount() + "")
                .withString("id", item.getSh_product_id()).navigation());
    }
}
